const state = {
  visitedViewList: [],
  noCachedViewList: ["Redirect"]
}

const mutations = {
  ADD_NO_CACHED_VIEW: (state, view) => {
    let f = state.noCachedViewList.some(v => v === view.name)
    if (!f){
      state.noCachedViewList.push(
          view.name
      )
    }
  },
  DEL_NO_CACHED_VIEW: (state, view) => {
    if(state.noCachedViewList.length>0){
      const index = state.noCachedViewList.indexOf(view.name)
      index > -1 && state.noCachedViewList.splice(index, 1)
    }
  },

  ADD_VISIT_VIEW: (state, view) => {
    let f = state.visitedViewList.some(v => v === view.name)
    if (!f){
      state.visitedViewList.push(view.name)
    }
  },

  DEL_VISIT_VIEW: (state, view) => {
    if(state.visitedViewList.length>0){
      const index = state.visitedViewList.indexOf(view.name)
      index > -1 && state.visitedViewList.splice(index, 1)
    }
  },

  ADD_VISIT_VIEW_ALL: (state, viewList) => {
      state.visitedViewList = viewList.map(item => item.name)
  },
}

const actions = {
  delNoCacheView({ commit }, view) {
    commit('DEL_NO_CACHED_VIEW', view)
  },

  addNoCacheView({ commit }, view) {
    return new Promise(resolve => {
      commit('ADD_NO_CACHED_VIEW', view)
      resolve([...state.noCachedViewList])
    })
  },


  addVisitView({ commit }, view) {
    return new Promise(resolve => {
      commit('ADD_VISIT_VIEW', view)
      resolve([...state.visitedViewList])
    })
  },

  delVisitView({ commit }, view) {
    return new Promise(resolve => {
      commit('DEL_VISIT_VIEW', view)
      resolve([...state.visitedViewList])
    })
  },

  addVisitViewAll({ commit }, viewList) {
    return new Promise(resolve => {
      commit('ADD_VISIT_VIEW_ALL', viewList)
      resolve([...state.visitedViewList])
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
